package com.neu.questionnaireplus.config;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


/** 
* @Description: 访问拦截器 
* @Param:  
* @return:  
* @Author: ~FHX~ (:TSunS:)
* @Date: 2022/10/26 
*/ 
public class VisiterInterceptor implements HandlerInterceptor {


  @Override
  public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
      Exception arg3)
      throws Exception {
    // TODO Auto-generated method stub
    //在整个请求结束之后被调用，也就是在DispatcherServlet 渲染了对应的视图之后执行，主要是用于进行资源清理工作
  }

  @Override
  public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2,
      ModelAndView arg3)
      throws Exception {
    // TODO Auto-generated method stub
    //请求处理之后进行调用，但是在视图被渲染之前，即Controller方法调用之后
  }

  
//  /**
//  * @Description: 在Controller运行前对方法进行拦截
//  * @Param: [request, response, object]
//  * @return: boolean
//  * @Author: ~FHX~ (:TSunS:)
//  * @Date: 2022/10/26
//  */
//  @Override
//  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object)
//      throws Exception {
//    response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
//    response.setHeader("Access-Control-Allow-Headers",
//        "token,Accept,Origin,XRequestedWith,Content-Type,LastModified");
//    //controller方法调用之前
//    String url = request.getRequestURI();
//
//    String tokenHeader = request.getHeader("token");
//
//    if (url.indexOf("admin") >= 0) {
//      //登录成功后将uesr信息存入session，以验证是否登录
//      if (tokenHeader != null) {
//        HttpSession session = MySessionContext.getSession(tokenHeader);
//        if (session != null && session.getAttribute("admin") != null) {
//          request.setAttribute("admin", session.getAttribute("admin"));
//          return true;
//        }
//      } else {
//        //TODO
//      }
//      JSONObject json = new JSONObject();
//      json.put("code", -1);
//      json.put("msg", "token expired or not login");
//      response.setContentType("application/json");
//      response.getWriter().write(json.toString());
//      return false;
//    } else {
//      return true;
//    }
//  }

  /**
   * @Description: 在Controller运行前对方法进行拦截
   * @Param: [request, response, object]
   * @return: boolean
   * @Author: ~FHX~ (:TSunS:)
   * @Date: 2022/10/26
   */
  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object)
      throws Exception {
//    String url = request.getRequestURI();
//    if (url.indexOf("login") >= 0){
//      return true;
//    }else {
//      return false;
//    }
    return true;
  }

}


